import { dynamicRouting, constantRoutes } from '@/router/index'
import { getPermission } from '@/api/user'
import store from '@/store/index'
const state = {
  routes: []
}
const mutations = {
  // 设置routes的值
  setRoutes(state, routes) {
    state.routes = [...constantRoutes, ...routes]
  }
}
const actions = {
  async getPermission() {
    const permissions = await getPermission()
    store.commit('user/savePermissionsP', permissions)
  },
  filterRoutes(store, menus) {
    const routes = dynamicRouting.filter(item => {
      return menus.includes(item.name)
    })
    // console.log(32, '具有访问权限的路由数据', routes)
    // 把筛选出来的数据保存在Vuex中
    store.commit('setRoutes', routes)
    return routes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
